/*
 * @(#)RelatedObjectInfoTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.taglib;

import java.io.StringWriter;
import java.io.Writer;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.dms.dao.DmsDocumentDAObject;
import com.dcivision.dms.taglib.DocumentInfoTag;
import com.dcivision.form.bean.FormData;
import com.dcivision.form.core.FormSubmissionManager;
import com.dcivision.form.dao.FormDataElementDAObject;
import com.dcivision.form.dao.FormRecordDAObject;
import com.dcivision.form.dao.FormSubmissionDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.PermissionManager;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.taglib.html.FormTag;
import com.dcivision.framework.web.WebUtil;
import com.dcivision.workflow.bean.MtmWorkflowChildParentProgress;
import com.dcivision.workflow.bean.MtmWorkflowProgressSystemObject;
import com.dcivision.workflow.bean.WorkflowProgress;
import com.dcivision.workflow.bean.WorkflowRecord;
import com.dcivision.workflow.bean.WorkflowStep;
import com.dcivision.workflow.dao.MtmWfStepFormRecordDAObject;
import com.dcivision.workflow.dao.MtmWorkflowChildParentProgressDAObject;
import com.dcivision.workflow.dao.WorkflowProgressDAObject;
import com.dcivision.workflow.dao.WorkflowRecordDAObject;
import com.dcivision.workflow.dao.WorkflowStepDAObject;
import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.bean.DmsVersion;

/**
 * @author           Dick xie
 * @company          DCIVision Limited
 * @creation date    2005/1/3
 * @version          $Revision: 1.42.2.9 $
 */
public class RelatedObjectInfoTag extends BodyTagSupport {
  public static final String REVISION = "$Revision: 1.42.2.9 $";

  protected Log log = LogFactory.getLog(this.getClass().getName());

  protected SessionContainer sessionContainer;
  protected String contextPath;
  private Integer trackID;
  private String styleClass;
  private int limitLength =-1;
  private boolean relatedItem = true;
  private boolean hasIframe = false;
  private String navMode = null;
  private boolean tracking = false;
  private boolean homePage = false;
  private String defaultFormRecordID = null;
  private String documentDeletedMessage;
  private String documentArchivedMessage;

  private WorkflowProgress workflowProgress;

  public RelatedObjectInfoTag(){
    super();
  }

  public boolean isRelatedItem() {
    return relatedItem;
  }

  public void setRelatedItem(boolean relatedItem) {
    this.relatedItem = relatedItem;
  }

  public boolean isHasIframe() {
    return hasIframe;
  }

  public void setHasIframe(boolean hasIframe) {
    this.hasIframe = hasIframe;
  }

  public Integer getTrackID() {
    return trackID;
  }

  public void setTrackID(Integer trackID) {
    this.trackID = trackID;
  }

  public int getLimitLength() {
    return limitLength;
  }

  public void setLimitLength(int limitLength) {
    this.limitLength = limitLength;
  }

  public String getStyleClass() {
    return styleClass;
  }

  public void setStyleClass(String styleClass) {
    this.styleClass = styleClass;
  }

  public WorkflowProgress getWorkflowProgress() {
    return workflowProgress;
  }

  public void setWorkflowProgress(WorkflowProgress workflowProgress) {
    this.workflowProgress = workflowProgress;
  }

  public String getNavMode() {
    return navMode;
  }

  public void setNavMode(String navMode) {
    this.navMode = navMode;
  }

  public boolean isTracking() {
    return tracking;
  }

  public void setTracking(boolean tracking) {
    this.tracking = tracking;
  }

    public boolean isHomePage() {
        return homePage;
    }

    public void setHomePage(boolean homePage) {
        this.homePage = homePage;
    }

    public String getDefaultFormRecordID() {
        return defaultFormRecordID;
    }

    public void setDefaultFormRecordID(String defaultFormRecordID) {
        this.defaultFormRecordID = defaultFormRecordID;
    }

  public int doStartTag() throws JspException {
    try {
      init();
      this.getContent() ;
    } catch (ApplicationException ae){
log.error(ae.getMsgCode(), ae);
    } catch (Exception e) {
log.fatal(e.getMessage(), e);
    } finally {
      this.release();
    }
    return(SKIP_BODY);
  }

  public int doEndTag() throws JspException {
    return EVAL_PAGE;
 }

  public void release() {
    super.release();
    trackID = null;
    workflowProgress = null;
    this.navMode = null;
    this.hasIframe = false;
  }

  protected boolean validateTag(){
    return ( trackID!=null || workflowProgress != null );
  }

  protected void getContent() throws Exception {
    JspWriter out = pageContext.getOut();
    String content = this.getRelatedObjectInfo();
    if (this.relatedItem == true) {
      //Bill add for EIP-2028 start
      //document has been deleted or archived.
      if(documentDeletedMessage.equals(content) || documentArchivedMessage.equals(content)){
        String relatedObject = MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "workflow.label.related_to_object");
        out.print("<html:img src='LayoutSpacer.gif' common='true' width='30' height='1'/><b style='color:#000066'>" + relatedObject + ": </b>");
        out.print("<b style='color:#FF0000'>"+content+"</b>");
      }
      //Bill add for EIP-2008 end

      //if not relate object info, then not out print the "Related Item" words.
      if (content.indexOf("<a") == -1) {
        content = "";
      } else {
        String relatedObject = MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "workflow.label.related_to_object");
        out.print("<html:img src='LayoutSpacer.gif' common='true' width='30' height='1'/><b style='color:#000066'>" + relatedObject + ": </b>");
      }
    }
    //Bill add for EIP-2028 start
    if(documentDeletedMessage.equals(content) || documentArchivedMessage.equals(content)){
      content="<b style='color:#FF0000'>"+content+"</b>";
    }
    //Bill add for EIP-2028 end
    out.println(content);
  }

  protected void showDocumentDeletedInfo(String content) throws Exception{
    if(content.indexOf(documentDeletedMessage)>-1){

    }
  }

  protected String getRelatedObjectInfo() throws Exception {
    StringBuffer sRelatedObjectInfo = new StringBuffer();
    String isInterrputWorkflowTrack = GlobalConstant.FALSE;
    List formList = new ArrayList();

    if(Utility.isEmpty(this.navMode)){
      this.setNavMode("V");
    }
    if ( !Utility.isEmpty(trackID)){
      sessionContainer = (SessionContainer)this.pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
      Connection conn = (Connection) pageContext.getRequest().getAttribute(GlobalConstant.DB_KEY);
      WorkflowStepDAObject stepDAO = new WorkflowStepDAObject(sessionContainer, conn);
      WorkflowRecordDAObject workflowRecordDAO = new WorkflowRecordDAObject(sessionContainer, conn);

      com.dcivision.form.dao.FormRecordDAObject formRecordDAO = new com.dcivision.form.dao.FormRecordDAObject(sessionContainer, conn);


      com.dcivision.workflow.dao.MtmWorkflowProgressSystemObjectDAObject mtmWfProgSysObjDAO = new com.dcivision.workflow.dao.MtmWorkflowProgressSystemObjectDAObject(sessionContainer, conn);
      FormSubmissionManager formSubmissionManager = new FormSubmissionManager(sessionContainer, conn);
      FormDataElementDAObject formDataElementDAO = new FormDataElementDAObject(sessionContainer, conn);

      String none = MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "common.label.none");

//    sRelatedObjectInfo.append("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">");
      String iframeURL = "";

      //Get the no submit form
      List pendingSubmitFormList = formRecordDAO.getPendingSubmitFormListByTrackID(trackID);
      for(int j=0; j<pendingSubmitFormList.size(); j++){
        String[] formItem = new String[2];

        sRelatedObjectInfo.append("<div class=\"relatedObject\">");
        com.dcivision.form.bean.FormRecord pendingForm = (com.dcivision.form.bean.FormRecord)pendingSubmitFormList.get(j);
        String tmpObjectName =  "";
        if (!Utility.isEmpty(pendingForm.getDisplayElementID()) && !"null".equals(pendingForm.getDisplayElementID()) && !"0".equals(pendingForm.getDisplayElementID().toString())){
          boolean hasPermission = formSubmissionManager.getRelatedItemPermissionByWorkflowRecordIDFormRecordIDUserRecordIDElementID(this.getWorkflowProgress().getWorkflowRecordID(),pendingForm.getID(),sessionContainer.getUserRecordID(),pendingForm.getDisplayElementID());
          if (hasPermission){
            List object = formDataElementDAO.getElementValueListBySubmissionIDElementID(pendingForm.getFormSubmissionID(),pendingForm.getDisplayElementID());
            for (int ii=0; ii<object.size(); ii++){
              FormData tmpFormData = (FormData)object.get(ii);
              tmpObjectName += ((tmpObjectName.length()>0)?",":"") + tmpFormData.getDataString();
            }
          }
        }
        if (Utility.isEmpty(tmpObjectName) || "null".equals(tmpObjectName)){
          tmpObjectName = pendingForm.getTitle() ;
        }

        if (this.limitLength != -1 && this.limitLength < tmpObjectName.length()) {
          tmpObjectName = tmpObjectName.substring(0,this.limitLength).replaceAll(" ", "&nbsp;")+"...";
        }

        iframeURL = contextPath + "/form/MaintFormSubmission.do?navMode="+ this.getNavMode() +"&formRecordID=" + pendingForm.getID() + "&formSubmissionID=" + pendingForm.getFormSubmissionID() + "&function=WORKFLOW&workflowStepID=" + (workflowProgress).getWorkflowStepID() + "&progressID=" + (workflowProgress).getID() + "&SHOW_HEADER_FOOTER=N&isViewTask=N";
        if (tracking){
          iframeURL += "&isShowWholeForm=true";
        }
        if(hasIframe){
            if(GlobalConstant.NAV_MODE_EDIT.equals(this.getNavMode())){
            formItem[0] = iframeURL + "&hideTitle=Y";
          } else {
            sRelatedObjectInfo.append("<img src=\"" + WebUtil.getImage(pageContext.getRequest(), "IconMenu-FMB_SUBMISSION_new.gif")+"\" align=\"absmiddle\" border=\"0\" height=\'16\' width=\'16\' > ");
            sRelatedObjectInfo.append("<a href=\"#\" class=\""+styleClass+"\"  onclick=\"");
            sRelatedObjectInfo.append(" iframeName.location.href=\'" + iframeURL +"&hideTitle=Y\'; showBlock(\'divFormSubmissionShow\'); hideBlock(\'divFormSubmissionHide\'); showBlock(\'formSubmission\');return false\">");
            sRelatedObjectInfo.append(tmpObjectName + "</a> ");
            formItem[0] = "iframeName.location.href=\'" + iframeURL +"&hideTitle=Y\'; showBlock(\'divFormSubmissionShow\'); hideBlock(\'divFormSubmissionHide\'); showBlock(\'formSubmission\');return false\"";
          }
        } else {
          sRelatedObjectInfo.append("<img src=\"" + WebUtil.getImage(pageContext.getRequest(), "IconMenu-FMB_SUBMISSION_new.gif")+"\" align=\"absmiddle\" border=\"0\" height=\'16\' width=\'16\' > ");

          //Home Page Task Inbox
          String mouseOver ="";
          if (isHomePage() && !isTracking()) {
            sRelatedObjectInfo.append("<a href=\"javascript://\" class=\""+styleClass+"\" "+mouseOver+" onClick=\"return OpEditProcessForForm(\'" + workflowProgress.getTrackID() + "\', \'" + workflowProgress.getID() + "\', \'" + workflowProgress.getWorkflowRecordID() + "\',\'" + pendingForm.getID() + "\');\">" +  tmpObjectName + "</a>");
          //Home Page Progress Monitor
          } else if (isHomePage() && isTracking()) {
            sRelatedObjectInfo.append("<a href=\"javascript://\" class=\""+styleClass+"\" "+mouseOver+" onClick=\"return OpViewTrack(\'" + workflowProgress.getTrackID() + "\', \'" + workflowProgress.getID() + "\', \'" + workflowProgress.getWorkflowRecordID() + "\',\'" + pendingForm.getID() + "\');\">" +  tmpObjectName + "</a>");
          } else {
            sRelatedObjectInfo.append("<a href=\"#\" class=\""+styleClass+"\" "+mouseOver+" onclick=\"return OpenCenteredPopup('" + iframeURL + "', 'DCIVisionWFPopUp',850,600,'status=no,menubar=no,scrollbars=yes,resizable=yes,toolbar=no,screenX=0,screenY=0,left=0,top=0');\">" +  tmpObjectName + "</a>");
          }
        }
        sRelatedObjectInfo.append("</div>");
        formItem[1] = tmpObjectName;
        formList.add(formItem);
      }

      List relatedObjList = mtmWfProgSysObjDAO.getListByTrackID(trackID);
      for (int i=0; i<relatedObjList.size(); i++) {
        com.dcivision.workflow.bean.MtmWorkflowProgressSystemObject relatedObj = (com.dcivision.workflow.bean.MtmWorkflowProgressSystemObject)relatedObjList.get(i);
        if (relatedObj != null && relatedObj.getParameterObject() != null) {
          java.util.Map dataMap = (java.util.Map)relatedObj.getParameterObject();
          Object workflowObj = dataMap.get("WORKFLOW_OBJECT");
          if (workflowObj instanceof DmsDocument) {
            sRelatedObjectInfo.append("<div class=\"relatedObject\">");
            DmsDocument dmsDocment = (DmsDocument)workflowObj;
            //Bill add for EIP-2028 start
            DmsDocumentDAObject dmsDocumentDAO = new DmsDocumentDAObject(this.sessionContainer, conn);
            DmsDocument tempDmsDocument=(DmsDocument)dmsDocumentDAO.getObjectByID(dmsDocment.getID());
            if(Utility.isEmpty(tempDmsDocument)){
              return documentDeletedMessage;
            }
            if(DmsVersion.ARCHIVED_STATUS.endsWith(tempDmsDocument.getItemStatus())){
              return documentArchivedMessage;
            }
            //Bill add for EIP-2028 end
            PermissionManager permissionManager = sessionContainer.getPermissionManager();
            String permission=permissionManager.getPermission(conn, GlobalConstant.OBJECT_TYPE_DOCUMENT,dmsDocment.getID());

            // Initialize the DocumentInfoTag
            DocumentInfoTag docTag = new DocumentInfoTag();
            docTag.setPageContext(this.pageContext);
            docTag.setParent(BodyTagSupport.findAncestorWithClass(this, FormTag.class));
            docTag.setDocObject(dmsDocment);
            //docTag.setPermission("R");      // falcon_delete
            docTag.setPermission(permission); // falcon_add_5980
            docTag.setRootDocument(null);
            docTag.setLimitLength(this.limitLength);
            //docTag.setIsOverLib(true);        // falcon_add
            docTag.setLinkFlag(true);
            docTag.setShowIndexLayer(true);

            // Begin to render the documentInfoTag content
            Writer docTagOut = new StringWriter();
            BodyContent bodyContent = pageContext.pushBody();
            docTag.doStartTag();
            docTag.doEndTag();
            docTag.release();
            pageContext.popBody();
            bodyContent.writeOut(docTagOut);
            // End of rendering documentInfoTag content

            sRelatedObjectInfo.append(docTagOut.toString());
            sRelatedObjectInfo.append("</div>");

            //********************* for Form
          } else if (workflowObj instanceof com.dcivision.form.bean.FormSubmission) {
              StringBuffer fromstr=getFormRelatedObject(relatedObj);
              sRelatedObjectInfo.append(fromstr);

           // ********************* for InterruptReq
          } else if (workflowObj instanceof com.dcivision.workflow.bean.MtmWorkflowTrackInterruptReq) {
              StringBuffer interRuptRelatedObject=getInterruptRelatedObject( workflowRecordDAO, dataMap);
              sRelatedObjectInfo.append(interRuptRelatedObject);
              isInterrputWorkflowTrack = GlobalConstant.TRUE;
          } else{
            sRelatedObjectInfo.append("<div class=\"relatedObject\">");
            sRelatedObjectInfo.append("<i>[ " + none + " ]</i>");
            sRelatedObjectInfo.append("</div>");
          }
        } else {
          sRelatedObjectInfo.append("<div class=\"relatedObject\">");
          sRelatedObjectInfo.append("<i>[ " + none + " ]</i>");
          sRelatedObjectInfo.append("</div>");
        }
      }

      //=====================================
      if (!Utility.isEmpty(formList) && GlobalConstant.NAV_MODE_EDIT.equals(this.getNavMode()) ) {
        sRelatedObjectInfo.append("<div class=\"relatedObject\">");
        if (formList.size() == 1) {
          String[] optionValue = (String[])(formList.get(0));
          sRelatedObjectInfo.append("<img src=\"" + WebUtil.getImage(pageContext.getRequest(), "IconMenu-FMB_SUBMISSION_new.gif")+"\" align=\"absmiddle\" border=\"0\" height=\'16\' width=\'16\' > ");
          sRelatedObjectInfo.append("<a href=\"#\" class=\""+styleClass+"\"  onclick=\"");
          sRelatedObjectInfo.append(" saveAndRefresh(\'" + optionValue[0] +"\'); \"> ");
          sRelatedObjectInfo.append(optionValue[1] + "</a> ");
        } else {
            sRelatedObjectInfo.append("<select style=\'Border-top-width=0; Border-right-width=0; Border-bottom-width=0; Border-left-width=0\' name=\'formRecordList\' onchange=\'saveAndRefresh(this.value)\'>");
            for (int i = 0; i < formList.size(); i++){
                String[] optionValue = (String[])(formList.get(i));
                sRelatedObjectInfo.append("<option value=\'" + optionValue[0] + "\'" + (optionValue[0].indexOf("formRecordID=" + this.getDefaultFormRecordID()) > 0? " selected " : "") + ">" + optionValue[1] + "</option>");
            }
            sRelatedObjectInfo.append("</select>");
        }
        sRelatedObjectInfo.append("</div>");
      }

      MtmWorkflowChildParentProgressDAObject childParentProgressDAO = new MtmWorkflowChildParentProgressDAObject(sessionContainer, conn);
      if (!Utility.isEmpty(this.workflowProgress)) {
          List childParentList = childParentProgressDAO.getListByParentWorkflowProgressID(this.workflowProgress.getID());
          if (!Utility.isEmpty(childParentList)) {
              WorkflowProgress fullProgress = null;
              WorkflowProgress pendingProgress = null;
              for (int k = 0; k < childParentList.size(); k++) {
                  MtmWorkflowChildParentProgress curChildParentProgress = (MtmWorkflowChildParentProgress)childParentList.get(k);
                  WorkflowProgressDAObject progressDAO = new WorkflowProgressDAObject(sessionContainer, conn);
                  List fullProgressList = progressDAO.getListByTrackID(curChildParentProgress.getSubworkflowTrackID());
                  // falcon_add_start_EIP-141
                  if (!Utility.isEmpty(fullProgressList)) {
                     for (int i = 0; i < fullProgressList.size(); i++){
                       fullProgress = (WorkflowProgress) fullProgressList.get(i);
                       if(!WorkflowStep.ACTION_TYPE_SUBMIT.equals(fullProgress.getActionType())) {
                         pendingProgress = (WorkflowProgress)fullProgressList.get(i);
                       }
                     }
                  }
                  // falcon_add_end
                  WorkflowProgress fristProgress = (WorkflowProgress)fullProgressList.get(0);
                  WorkflowStep subStep = (WorkflowStep)stepDAO.getObjectByID(fristProgress.getWorkflowStepID());
                  WorkflowRecord subRecord = (WorkflowRecord)workflowRecordDAO.getObjectByID(subStep.getWorkflowRecordID());
                  //String objectName = "Related Sub Workflow Track :" + curChildParentProgress.getSubworkflowTrackID()+ "." + subRecord.getWorkflowCode() + "[" +subRecord.getWorkflowVersion() + "]";
                  String objectName = MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "worklfow.related_sub_workflow_track") + TextUtility.formatNumberWithZeroPrefix(SystemParameterFactory.getSystemParameterInteger(SystemParameterConstant.WORKLFLOW_TRACK_ID_FORMAT_LENGTH).intValue(), curChildParentProgress.getSubworkflowTrackID())+ "<br>"+MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "worklfow.related_sub_workflow") + subRecord.getWorkflowCode() + "[" +subRecord.getWorkflowVersion() + "]";

                  // sub workflow
                  String overLibName="";
                  if (this.limitLength != -1 && this.limitLength < objectName.length()) {
                    overLibName=objectName;
                    objectName = objectName.substring(0, this.limitLength).replaceAll(" ", "&nbsp;") + "...";
                  }

                  String mouseOver ="";
                  if (!Utility.isEmpty(overLibName)) {
                     mouseOver = "  onmouseover=\"return getPopupInfo('"+overLibName+"');STICKY, MOUSEOFF\" onmouseout=\"return nd();\" ";
                  }

                  sRelatedObjectInfo.append("<div class=\"relatedObject\">");
                  sRelatedObjectInfo.append("<img src=\"" + WebUtil.getImage(pageContext.getRequest(), "IconMenu-WFM_SUBMISSION_new.gif")+"\" align=\"absmiddle\" border=\"0\" height=\'16\' width=\'16\' > ");
                  sRelatedObjectInfo.append("<a ");
                  sRelatedObjectInfo.append("href=\"#\" ");
                  if (!Utility.isEmpty(styleClass)){
                    sRelatedObjectInfo.append( "class=\""+styleClass+"\" " );
                  }
                  if (!Utility.isEmpty(fullProgress) && !Utility.isEmpty(pendingProgress) && !WorkflowStep.ACTION_TYPE_SUBMIT.equals(fullProgress.getActionType())) {
                    sRelatedObjectInfo.append(""+mouseOver+" onClick=\"return OpenCenteredPopup('" + contextPath + "/workflow/MaintWorkflowTrack.do?showFullList=Y&navMode=V&SHOW_HEADER_FOOTER=N&workflowProgressID=" +pendingProgress.getID().toString() + "&trackID=" + curChildParentProgress.getSubworkflowTrackID().toString() + "&ID=" + subStep.getWorkflowRecordID().toString() + "&workflowStepID=" + subStep.getID().toString() + "', 'DCIVisionWFPopUp',800,600,'status=no,menubar=no,scrollbars=yes,resizable=yes,toolbar=no,screenX=0,screenY=0,left=0,top=0');\">" +  objectName + "</a>");
                  }else {
                    sRelatedObjectInfo.append(""+mouseOver+" onClick=\"return OpenCenteredPopup('" + contextPath + "/workflow/MaintWorkflowTrack.do?showFullList=Y&navMode=V&SHOW_HEADER_FOOTER=N&workflowProgressID=" +fristProgress.getID().toString() + "&trackID=" + curChildParentProgress.getSubworkflowTrackID().toString() + "&ID=" + subStep.getWorkflowRecordID().toString() + "&workflowStepID=" + subStep.getID().toString() + "', 'DCIVisionWFPopUp',800,600,'status=no,menubar=no,scrollbars=yes,resizable=yes,toolbar=no,screenX=0,screenY=0,left=0,top=0');\">" +  objectName + "</a>");
                  }
                  sRelatedObjectInfo.append("</div>");
              }
          }

          childParentList = childParentProgressDAO.getListByTrackID(this.workflowProgress.getTrackID());
          if (GlobalConstant.FALSE.equals(isInterrputWorkflowTrack)) {
            if (!Utility.isEmpty(childParentList)) {
              for (int k = 0; k < childParentList.size(); k++) {
                MtmWorkflowChildParentProgress curChildParentProgress = (MtmWorkflowChildParentProgress)childParentList.get(k);
                WorkflowProgressDAObject progressDAO = new WorkflowProgressDAObject(sessionContainer, conn);
                WorkflowProgress parentProgress = (WorkflowProgress) progressDAO.getObjectByID(curChildParentProgress.getParentWorkflowProgressID());
                WorkflowStep parentStep = (WorkflowStep)stepDAO.getObjectByID(parentProgress.getWorkflowStepID());
                WorkflowRecord parentRecord = (WorkflowRecord)workflowRecordDAO.getObjectByID(parentStep.getWorkflowRecordID());
                //String objectName = "Related Parent Workflow Track :" + parentProgress.getTrackID()+ "." + parentRecord.getWorkflowCode() + "[" +parentRecord.getWorkflowVersion() + "]";
                String objectName = MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "worklfow.related_parent_workflow_track") + TextUtility.formatNumberWithZeroPrefix(SystemParameterFactory.getSystemParameterInteger(SystemParameterConstant.WORKLFLOW_TRACK_ID_FORMAT_LENGTH).intValue(), parentProgress.getTrackID())+ "<br>"+MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "worklfow.related_parent_workflow")  + parentRecord.getWorkflowCode() + "[" +parentRecord.getWorkflowVersion() + "]";

                String overLibName="";
                if (this.limitLength != -1 && this.limitLength < objectName.length()) {
                  overLibName=objectName;
                  objectName = objectName.substring(0, this.limitLength).replaceAll(" ", "&nbsp;") + "...";
                }

                String mouseOver ="";
                if (!Utility.isEmpty(overLibName)) {
                   mouseOver = "  onmouseover=\"return getPopupInfo('"+overLibName+"');STICKY, MOUSEOFF\" onmouseout=\"return nd();\" ";
                }

                sRelatedObjectInfo.append("<div class=\"relatedObject\">");
                sRelatedObjectInfo.append("<img src=\"" + WebUtil.getImage(pageContext.getRequest(), "IconMenu-WFM_SUBMISSION_new.gif")+"\" align=\"absmiddle\" border=\"0\" height=\'16\' width=\'16\' > ");
                sRelatedObjectInfo.append("<a ");
                sRelatedObjectInfo.append("href=\"#\" ");
                if (!Utility.isEmpty(styleClass)){
                  sRelatedObjectInfo.append( "class=\""+styleClass+"\" " );
                }
                sRelatedObjectInfo.append(""+mouseOver+" onClick=\"return OpenCenteredPopup('" + contextPath + "/workflow/MaintWorkflowTrack.do?showFullList=Y&navMode=V&SHOW_HEADER_FOOTER=N&workflowProgressID=" +parentProgress.getID().toString() + "&trackID=" + parentProgress.getTrackID().toString() + "&ID=" + parentStep.getWorkflowRecordID().toString() + "&workflowStepID=" + parentStep.getID().toString() + "', 'DCIVisionWFPopUp',800,600,'status=no,menubar=no,scrollbars=yes,resizable=yes,toolbar=no,screenX=0,screenY=0,left=0,top=0');\">" +  objectName + "</a>");
                sRelatedObjectInfo.append("</div>");
              }
            }
          }
      } else if (!Utility.isEmpty(this.trackID)) {
          List childParentList = childParentProgressDAO.getListByTrackID(this.workflowProgress.getTrackID());
          if (GlobalConstant.FALSE.equals(isInterrputWorkflowTrack)) {
            if (!Utility.isEmpty(childParentList)) {
                for (int k = 0; k < childParentList.size(); k++) {
                    MtmWorkflowChildParentProgress curChildParentProgress = (MtmWorkflowChildParentProgress)childParentList.get(k);
                    WorkflowProgressDAObject progressDAO = new WorkflowProgressDAObject(sessionContainer, conn);
                    WorkflowProgress parentProgress = (WorkflowProgress) progressDAO.getObjectByID(curChildParentProgress.getParentWorkflowProgressID());
                    WorkflowStep parentStep = (WorkflowStep)stepDAO.getObjectByID(parentProgress.getWorkflowStepID());
                    WorkflowRecord parentRecord = (WorkflowRecord)workflowRecordDAO.getObjectByID(parentStep.getWorkflowRecordID());
                    //String objectName = "Related Parent Workflow Track :" + parentProgress.getTrackID()+ "." + parentRecord.getWorkflowCode() + "[" +parentRecord.getWorkflowVersion() + "]";
                    String objectName = MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "worklfow.related_parent_workflow_track") + TextUtility.formatNumberWithZeroPrefix(SystemParameterFactory.getSystemParameterInteger(SystemParameterConstant.WORKLFLOW_TRACK_ID_FORMAT_LENGTH).intValue(), parentProgress.getTrackID())+ "<br>"+MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "worklfow.related_parent_workflow")  + parentRecord.getWorkflowCode() + "[" +parentRecord.getWorkflowVersion() + "]";

                    String overLibName="";
                    if (this.limitLength != -1 && this.limitLength < objectName.length()) {
                      overLibName=objectName;
                      objectName = objectName.substring(0, this.limitLength).replaceAll(" ", "&nbsp;") + "...";
                    }

                    String mouseOver ="";
                    if (!Utility.isEmpty(overLibName)) {
                       mouseOver = "  onmouseover=\"return getPopupInfo('"+overLibName+"');STICKY, MOUSEOFF\" onmouseout=\"return nd();\" ";
                    }

                    sRelatedObjectInfo.append("<div class=\"relatedObject\">");
                    sRelatedObjectInfo.append("<img src=\"" + WebUtil.getImage(pageContext.getRequest(), "IconMenu-WFM_SUBMISSION_new.gif")+"\" align=\"absmiddle\" border=\"0\" height=\'16\' width=\'16\' > ");
                    sRelatedObjectInfo.append("<a ");
                    sRelatedObjectInfo.append("href=\"#\" ");
                    if (!Utility.isEmpty(styleClass)){
                      sRelatedObjectInfo.append( "class=\""+styleClass+"\" " );
                    }
                    sRelatedObjectInfo.append(""+mouseOver+" onClick=\"return OpenCenteredPopup('" + contextPath + "/workflow/MaintWorkflowTrack.do?showFullList=Y&navMode=V&SHOW_HEADER_FOOTER=N&workflowProgressID=" +parentProgress.getID().toString() + "&trackID=" +  parentProgress.getTrackID().toString()+ "&ID=" + parentStep.getWorkflowRecordID().toString() + "&workflowStepID=" + parentStep.getID().toString() + "', 'DCIVisionWFPopUp',800,600,'status=no,menubar=no,scrollbars=yes,resizable=yes,toolbar=no,screenX=0,screenY=0,left=0,top=0');\">" +  objectName + "</a>");
                    sRelatedObjectInfo.append("</div>");
                }
            }
          }
      }
//      sRelatedObjectInfo.append("</table>");
//      sRelatedObjectInfo.append("</nobr>");
    }

    return sRelatedObjectInfo.toString();
  }

  private StringBuffer getInterruptRelatedObject( WorkflowRecordDAObject workflowRecordDAO, java.util.Map dataMap) {
     StringBuffer sRelatedObjectInfo=new StringBuffer();
     String overLibName="";

      sRelatedObjectInfo.append("<div class=\"relatedObject\">");
      WorkflowProgress wfProgress = (WorkflowProgress)dataMap.get("WORKFLOW_PROGRESS");
      try{
        WorkflowRecord wfRecord = (WorkflowRecord)workflowRecordDAO.getObjectByID(wfProgress.getWorkflowRecordID());
        //String objectName = wfRecord.getWorkflowCode() + "-" + wfProgress.getTrackID();
        String objectName = wfRecord.getWorkflowCode() + "-" + TextUtility.formatNumberWithZeroPrefix(SystemParameterFactory.getSystemParameterInteger(SystemParameterConstant.WORKLFLOW_TRACK_ID_FORMAT_LENGTH).intValue(), wfProgress.getTrackID());
        if (this.limitLength != -1 && this.limitLength < objectName.length()) {
          overLibName=objectName;
          objectName = objectName.substring(0,this.limitLength).replaceAll(" ", "&nbsp;")+"...";
        }

        String mouseOver ="";
          if (!Utility.isEmpty(overLibName)) {
             mouseOver = "  onmouseover=\"return getPopupInfo('"+overLibName+"');STICKY, MOUSEOFF\" onmouseout=\"return nd();\" ";
          }

        sRelatedObjectInfo.append("<img src=\"" + WebUtil.getImage(pageContext.getRequest(), "IconMenu-WFM_SUBMISSION_new.gif")+"\" align=\"absmiddle\" border=\"0\" height=\'16\' width=\'16\' > ");
        sRelatedObjectInfo.append("<a ");
        sRelatedObjectInfo.append("href=\"#\" ");
        if (!Utility.isEmpty(styleClass)){
          sRelatedObjectInfo.append( "class=\""+styleClass+"\" " );
        }
        if (tracking) {
          sRelatedObjectInfo.append(""+mouseOver+"onClick=\"return OpViewTrack('" + wfProgress.getTrackID() + "', '" + wfProgress.getID() + "', '" + wfProgress.getWorkflowRecordID() + "');\">" + objectName + "</a>");
        }else {
          sRelatedObjectInfo.append(""+mouseOver+"onClick=\"return OpViewProcess('" + wfProgress.getTrackID() + "', '" + wfProgress.getID() + "', '" + wfProgress.getWorkflowRecordID() + "');\">" + objectName + "</a>");
        }
      } catch (ApplicationException ae) {
        sRelatedObjectInfo.append("<strike>").append(wfProgress.getWorkflowCode()).append("</strike>");
log.debug(ae.toString()+":"+wfProgress.getWorkflowRecordID()+";"+wfProgress.getWorkflowCode());
      }
      sRelatedObjectInfo.append("</div>");
      return sRelatedObjectInfo;
  }


  private StringBuffer getFormRelatedObject( MtmWorkflowProgressSystemObject relatedObj) throws ApplicationException {
    String iframeURL;
    List formList = new ArrayList();
    StringBuffer sTempInfo = new StringBuffer();
    String overLibName="";

    StringBuffer sRelatedObjectInfo=new StringBuffer();
    sessionContainer = (SessionContainer)this.pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    Connection conn = (Connection) pageContext.getRequest().getAttribute(GlobalConstant.DB_KEY);
    WorkflowStepDAObject stepDAO = new WorkflowStepDAObject(sessionContainer, conn);
    MtmWfStepFormRecordDAObject stepFormRecordDAO = new MtmWfStepFormRecordDAObject(sessionContainer, conn);
    FormRecordDAObject formRecordDAO = new com.dcivision.form.dao.FormRecordDAObject(sessionContainer, conn);
    FormSubmissionDAObject formSubmissionDAO = new FormSubmissionDAObject(sessionContainer, conn);
    com.dcivision.form.bean.FormSubmission formSub = (com.dcivision.form.bean.FormSubmission)formSubmissionDAO.getObjectByID(relatedObj.getObjectID() );
    FormSubmissionManager formSubmissionManager = new FormSubmissionManager(sessionContainer, conn);
    FormDataElementDAObject formDataElementDAO = new FormDataElementDAObject(sessionContainer, conn);
    WorkflowStep currentStep = (WorkflowStep)stepDAO.getObjectByID(this.getWorkflowProgress().getWorkflowStepID());
    List objectList = formSubmissionDAO.getHasPermissionListByTrackIDWorkflowStepID(trackID, currentStep.getID());

    if (!Utility.isEmpty(objectList)) {
      sRelatedObjectInfo.append("<div class=\"relatedObject\">");
      try {
          com.dcivision.form.bean.FormRecord formRecord = (com.dcivision.form.bean.FormRecord)formRecordDAO.getObjectByID(formSub.getFormRecordID());
          String objectName =  "";
          if (!Utility.isEmpty(formRecord.getDisplayElementID()) && !"null".equals(formRecord.getDisplayElementID()) && !"0".equals(formRecord.getDisplayElementID().toString())){
            boolean hasPermission = formSubmissionManager.getRelatedItemPermissionByWorkflowRecordIDFormRecordIDUserRecordIDElementID(this.getWorkflowProgress().getWorkflowRecordID(),formRecord.getID(),sessionContainer.getUserRecordID(),formRecord.getDisplayElementID());
            if (hasPermission){
              List object = formDataElementDAO.getElementValueListBySubmissionIDElementID(formSub.getID(),formRecord.getDisplayElementID());
              for (int ii=0; ii<object.size(); ii++){
                FormData tmpFormData = (FormData)object.get(ii);
                objectName += ((objectName.length()>0)?",":"") + tmpFormData.getDataString();
              }
            }
          }
          if (Utility.isEmpty(objectName) || "null".equals(objectName)){
            objectName = formRecord.getTitle() ;
          }
          if (this.limitLength != -1 && this.limitLength < objectName.length()) {
            overLibName=objectName;
            objectName = objectName.substring(0,this.limitLength).replaceAll(" ", "&nbsp;")+"...";
          }

          String mouseOver ="";
          if (!Utility.isEmpty(overLibName)) {
             mouseOver = "  onmouseover=\"return getPopupInfo('"+overLibName+"');STICKY, MOUSEOFF\" onmouseout=\"return nd();\" ";
          }

          if (Utility.isEmpty(formRecord.getFormDocID()) || "null".equals(formRecord.getFormDocID())){
            //if the first step no binding form permission, and the current step has permission.
            if (WorkflowStep.ACTION_TYPE_SUBMIT.equals(currentStep.getActionType()) || stepFormRecordDAO.hasPermissionByWorkflowStepIDFormRecordID(this.getWorkflowProgress().getWorkflowStepID(), formRecord.getID())) {
              String[] formItem = new String[2];
              sRelatedObjectInfo.append(sTempInfo);
              //iframeURL =  contextPath + "/form/MaintFormSubmission.do?navMode=V&formRecordID=" + formSub.getFormRecordID() + "&formSubmissionID=" + formSub.getID() + "&workflowStepID=" + ((WorkflowProgress)workflowProgress).getStepSeq() + "&workflowStepIDStep=" + ((WorkflowProgress)workflowProgress).getWorkflowStepID() + "&progressID=" + ((WorkflowProgress)workflowProgress).getID() + "&function=FORMTRACKING&isWorkflowViewFlag=Y&SHOW_HEADER_FOOTER=N&isViewTask=Y";
              iframeURL = contextPath + "/form/MaintFormSubmission.do?navMode="+ this.getNavMode() +"&formRecordID=" + formSub.getFormRecordID() + "&formSubmissionID=" + formSub.getID() + "&function=WORKFLOW&workflowStepID=" + (workflowProgress).getWorkflowStepID() + "&progressID=" + (workflowProgress).getID() + "&SHOW_HEADER_FOOTER=N&isViewTask=N";
              if (tracking){
                iframeURL += "&isShowWholeForm=true";
              }

              if(!Utility.isEmpty(iframeURL) && hasIframe){
                if(GlobalConstant.NAV_MODE_EDIT.equals(this.getNavMode())){
                  //bill add start for EIP-1134
                  sRelatedObjectInfo.append("<img src=\"" + WebUtil.getImage(pageContext.getRequest(), "IconMenu-FMB_SUBMISSION_new.gif")+"\" align=\"absmiddle\" border=\"0\" height=\'16\' width=\'16\' > ");
                  sRelatedObjectInfo.append(objectName);
                  //bill add end for EIP-1134
                  formItem[0] = iframeURL + "&hideTitle=Y";
                } else {
                  sRelatedObjectInfo.append("<img src=\"" + WebUtil.getImage(pageContext.getRequest(), "IconMenu-FMB_SUBMISSION_new.gif")+"\" align=\"absmiddle\" border=\"0\" height=\'16\' width=\'16\' > ");
                  sRelatedObjectInfo.append(" <a href=\"#\" ");
                  if (!Utility.isEmpty(styleClass)){
                    sRelatedObjectInfo.append ( "class=\""+styleClass+"\" " );
                  }
                  sRelatedObjectInfo.append(" onclick=\"");
                  sRelatedObjectInfo.append(" iframeName.location.href=\'" + iframeURL +"&hideTitle=Y\'; showBlock(\'divFormSubmissionShow\'); hideBlock(\'divFormSubmissionHide\'); showBlock(\'formSubmission\');return false\">");
                  sRelatedObjectInfo.append(objectName + "</a> ");
                  formItem[0] =  "iframeName.location.href=\'" + iframeURL +"&hideTitle=Y\'; showBlock(\'divFormSubmissionShow\'); hideBlock(\'divFormSubmissionHide\'); showBlock(\'formSubmission\');return false\">";
                }
              } else {
                sRelatedObjectInfo.append ("<img src=\"" + WebUtil.getImage(pageContext.getRequest(), "IconMenu-FMB_SUBMISSION_new.gif")+"\" align=\"absmiddle\" border=\"0\" height=\'16\' width=\'16\' > ");

                //Home Page Task Inbox
                if (isHomePage() && !isTracking()) {
                  //sRelatedObjectInfo.append("<a href=\"javascript://\" class=\""+styleClass+"\" "+mouseOver+"  onClick=\"return OpEditProcess(\'" + workflowProgress.getTrackID() + "\', \'" + workflowProgress.getID() + "\', \'" + workflowProgress.getWorkflowRecordID() + "\',\'" + formRecord.getID() + "\');\">" +  objectName + "</a>");//simon_del
                  sRelatedObjectInfo.append("<a href=\"javascript://\" class=\""+styleClass+"\" "+mouseOver+"  onClick=\"return OpEditProcessForForm(\'" + workflowProgress.getTrackID() + "\', \'" + workflowProgress.getID() + "\', \'" + workflowProgress.getWorkflowRecordID() + "\',\'" + formRecord.getID() + "\');\">" +  objectName + "</a>");//simon_add
                //Home Page Progress Monitor
                } else if (isHomePage() && isTracking()) {
                  sRelatedObjectInfo.append("<a href=\"javascript://\" class=\""+styleClass+"\" "+mouseOver+" onClick=\"return OpViewTrack(\'" + workflowProgress.getTrackID() + "\', \'" + workflowProgress.getID() + "\', \'" + workflowProgress.getWorkflowRecordID() + "\',\'" + formRecord.getID() + "\');\">" +  objectName + "</a>");
                } else {
                  sRelatedObjectInfo.append (" <a href=\"#\" ");
                  if (!Utility.isEmpty(styleClass)){
                    sRelatedObjectInfo.append ( "class=\""+styleClass+"\" " );
                  }
                  // Hidden the navigation when it is opened in popup window.
                  if (!Utility.isEmpty(iframeURL) && iframeURL.indexOf("hideTitle") < 0) {
                    if (iframeURL.indexOf("?") < 0) {
                      iframeURL += "?hideTitle=Y";
                    } else {
                      iframeURL += "&hideTitle=Y";
                    }
                  }
                  sRelatedObjectInfo.append(""+mouseOver+"onclick=\"return OpenCenteredPopup('" + iframeURL + "', 'DCIVisionWFPopUp',850,600,'status=no,menubar=no,scrollbars=yes,resizable=yes,toolbar=no,screenX=0,screenY=0,left=0,top=0');\">" +  objectName + "</a>");
                }
              }
              formItem[1] = objectName;
              formList.add(formItem);
            }

          } else {
            sRelatedObjectInfo.append ("<img src=\"" + WebUtil.getImage(pageContext.getRequest(), "IconDMSFileExtPDF.gif")+"\" align=\"absmiddle\" border=\"0\"> <a href=\"#\" ");
            //Home Page Task Inbox
            if (isHomePage() && !isTracking()) {
              sRelatedObjectInfo.append("<a href=\"javascript://\" class=\""+styleClass+"\" "+mouseOver+" onClick=\"return OpEditProcessForForm(\'" + workflowProgress.getTrackID() + "\', \'" + workflowProgress.getID() + "\', \'" + workflowProgress.getWorkflowRecordID() + "\',\'" + formRecord.getID() + "\');\">" +  objectName + "</a>");//simon_add
            //Home Page Progress Monitor
            } else if (isHomePage() && isTracking()) {
              sRelatedObjectInfo.append("<a href=\"javascript://\" class=\""+styleClass+"\" "+mouseOver+" onClick=\"return OpViewTrack(\'" + workflowProgress.getTrackID() + "\', \'" + workflowProgress.getID() + "\', \'" + workflowProgress.getWorkflowRecordID() + "\',\'" + formRecord.getID() + "\');\">" +  objectName + "</a>");
            } else {
              if (!Utility.isEmpty(styleClass)){
                sRelatedObjectInfo.append ( "class=\"" + styleClass + "\" " );
              }
              sRelatedObjectInfo.append(sTempInfo);
              sRelatedObjectInfo.append(""+mouseOver+" onclick=\"return OpenCenteredPopup('" + contextPath + "/form/GenerateXML.do?navMode="+ this.getNavMode() +"&formRecordID=" + formSub.getFormRecordID() + "&formSubmissionID=" + formSub.getID() + "&formDocID=" + formRecord.getFormDocID() + "&workflowStepID=" + (workflowProgress).getWorkflowStepID() + "&isViewTask="+ (GlobalConstant.NAV_MODE_EDIT.equals(this.navMode)? "N" : "Y") +"&progressID=" + (workflowProgress).getID() + "&status=R&step=N', 'DCIVisionWFPopUp',1016,748,'status=no,menubar=no,scrollbars=yes,resizable=yes,toolbar=no,screenX=0,screenY=0,left=0,top=0');\">" +  objectName + "</a>");
            }
          }

      } catch (ApplicationException ae) {
        sRelatedObjectInfo.append("<strike>").append(formSub.getFormCode()).append("</strike>");
log.debug(ae.toString()+":"+formSub.getFormRecordID()+";"+formSub.getFormCode());
      }
      sRelatedObjectInfo.append("</div>");
    }
    return sRelatedObjectInfo;
  }

  protected void init() throws ApplicationException {
    sessionContainer = (SessionContainer)this.pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    contextPath = (String)this.pageContext.getServletContext().getAttribute(GlobalConstant.CONTEXT_PATH_KEY);
    documentDeletedMessage=MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "dms.message.document_deleted");
    documentArchivedMessage=MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "dms.message.document_archived");
    if ( !validateTag() ){
      throw new IllegalArgumentException();
    }

  }

}


